package com.igeekhome.controller.api;

import com.github.pagehelper.PageInfo;
import com.igeekhome.pojo.SysStaff;
import com.igeekhome.pojo.SysStaffEnclose;
import com.igeekhome.service.IFileUploadService;
import com.igeekhome.service.ISysStaffEncloseService;
import com.igeekhome.service.impl.SysStaffEncloseServiceImpl;
import com.igeekhome.util.CommonUtil;
import com.igeekhome.util.ResultBean;
import com.igeekhome.util.TableResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import sun.security.util.KeyUtil;

import javax.servlet.http.HttpServletRequest;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;

/**
 * @author 立以宁
 */
@RestController
@RequestMapping("/api/emp")
public class SysStaffEncloseControllerApi {

    @Autowired
    private SysStaffEncloseServiceImpl sysStaffEncloseService;

    @Autowired
    private IFileUploadService fileUploadService;

   /* @Value("${upload.picture.path}")
    private String filePath;

    @Value("${upload.picture.path.relative}")
    private String filePathRelative;*/

    /**
     *
     * @param file
     * @// TODO: 图片的异步上传，返回图片新路径进行回显
     * 2019/12/29
     * @return
     */
    @RequestMapping("/fileUpload")
    public ResultBean upload(@RequestParam("file")MultipartFile file) {

        String originalFilename = file.getOriginalFilename();
        FileInputStream inputStream = null;
        try {
            inputStream = (FileInputStream) file.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String src = fileUploadService.uploadFile(inputStream,originalFilename);
        // KeyUtil.genUniqueKey()生成图片的随机名
        HashMap map = new HashMap(16);
        map.put("src",src);

        return ResultBean.build(0,"上传文件成功",map);
    }





    @RequestMapping("/list")
    public TableResultBean empList(@RequestParam(defaultValue ="10") Integer limit, @RequestParam(defaultValue ="1")Integer page, SysStaffEnclose sysStaffEnclose) {
        PageInfo<SysStaffEnclose> pageInfo = sysStaffEncloseService.selectAll(limit, page, sysStaffEnclose);

        return new TableResultBean(0,"成功",pageInfo.getTotal(),pageInfo.getList());
    }

    @RequestMapping("/add")
    public ResultBean addEmp(SysStaffEnclose sysStaffEnclose, HttpServletRequest request) {
        System.out.println(sysStaffEnclose);
        SysStaff staff = new SysStaff();
        String name = request.getParameter("name");
        String affiliatedNetworkId = request.getParameter("affiliatedNetworkId");
        String staffId = request.getParameter("staffId");

        staff.setName(name);
        staff.setStaffId(Integer.valueOf(staffId));
        staff.setAffiliatedNetworkId(Integer.valueOf(affiliatedNetworkId));
        sysStaffEnclose.setSysStaff(staff);

        sysStaffEncloseService.add(sysStaffEnclose);
        return ResultBean.ok();
    }

    @RequestMapping("/delete/{id}")
    public ResultBean deleteEmp(Integer id) {
        sysStaffEncloseService.deleteByno(id);
        return ResultBean.ok();
    }


    @RequestMapping("/update")
    public ResultBean updateEmp(SysStaffEnclose sysStaffEnclose,HttpServletRequest request) {
        String name = request.getParameter("name");
        String affiliatedNetworkId = request.getParameter("affiliatedNetworkId");
        String staffId = request.getParameter("staffId");

        SysStaff staff = new SysStaff();
        staff.setName(name);
        staff.setStaffId(Integer.valueOf(staffId));
        staff.setAffiliatedNetworkId(Integer.valueOf(affiliatedNetworkId));
        sysStaffEnclose.setSysStaff(staff);

        sysStaffEncloseService.update(sysStaffEnclose);
        return ResultBean.ok();
    }

}
